package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Title: SessionController
 * @Package: com.project.controller
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/28 - 10:46
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("session")
public class SessionController {
    @RequestMapping("create")
    public String creatSession(HttpServletRequest req){
//        如果有存放该用户信息的session对象，则直接取出，否则为该用户创建session对象.
        HttpSession session=req.getSession();
//        在session中绑定共享对象，在一个会话范围内共享
        session.setAttribute("userName","tom");
////        设置最大保留时间
//        session.setMaxInactiveInterval(1800);
////        立即销毁
//        session.invalidate();



        return session.getId();
    }
    @RequestMapping("getInfo")
    public String getSessionInfo(HttpServletRequest req){
        HttpSession session= req.getSession();
//        从session中取出对象
       Object obj= session.getAttribute("userName");
       return obj.toString();
    }
}
